import Decimal from 'decimal.js'
const isNum = function (a) {
  return a !== '' && a !== null && !isNaN(a)
}

const filterNumber = function (array) {
  const res = []
  array.forEach(element => {
    if (isNum(element)) {
      res.push(+element)
    }
  })
  return res
}

export const number = function (val, def = 0) {
  if (!isNum(number)) {
    return def
  }
  return val
}

export const max = function (...args) {
  const arr = filterNumber(args)
  return Math.max.apply(null, arr)
}

export const min = function (...args) {
  const arr = filterNumber(args)
  return Math.min.apply(null, arr)
}

export const div = function (...args) {
  const arr = filterNumber(args)
  if (arr.length > 1) {
    return arr.reduce((a, b) => new Decimal(a).div(b)).toNumber()
  } else if (arr.length > 0) {
    return new Decimal(arr[0]).toNumber()
  } else {
    return 0
  }
}

export const add = function (...args) {
  const arr = filterNumber(args)
  if (arr.length > 1) {
    return arr.reduce((a, b) => new Decimal(a).add(b)).toNumber()
  } else if (arr.length > 0) {
    return new Decimal(arr[0]).toNumber()
  } else {
    return 0
  }
}

export const mul = function (...args) {
  const arr = filterNumber(args)
  if (arr.length > 1) {
    return arr.reduce((a, b) => new Decimal(a).mul(b)).toNumber()
  } else if (arr.length > 0) {
    return new Decimal(arr[0]).toNumber()
  } else {
    return 0
  }
}

export const sub = function (...args) {
  const arr = filterNumber(args)
  if (arr.length > 1) {
    return arr.reduce((a, b) => new Decimal(a).sub(b)).toNumber()
  } else if (arr.length > 0) {
    return new Decimal(arr[0]).toNumber()
  } else {
    return 0
  }
}

const roundMode = [Decimal.ROUND_HALF_UP, Decimal.ROUND_UP, Decimal.ROUND_DOWN]
export const round = (number, numDigits, upOrDown = 0) => {
  if (isNum(number)) {
    const nu = new Decimal(number)
    return nu.toDP(numDigits, roundMode[upOrDown]).toNumber()
  } else {
    return 0
  }
}

export const money = function (value, prefix = 2, def = 0) {
  const num = +value
  return (isNum(num) ? num : def).toLocaleString('zh', {
    style: 'currency',
    currency: 'CNY',
    minimumFractionDigits: prefix
  })
}

export const moneyNum = function (value, prefix = 2, def = 0) {
  let mon = money(value, prefix, def)
  return (mon + '').substr(1)
}

export const calc = function (start = 0) {
  let result = start
  const bus = {
    add (...args) {
      result = add(result, ...args)
      return bus
    },
    sub (...args) {
      result = sub(result, ...args)
      return bus
    },
    div (...args) {
      result = div(result, ...args)
      return bus
    },
    mul (...args) {
      result = mul(result, ...args)
      return bus
    },
    max (...args) {
      result = max(result, ...args)
      return bus
    },
    min (...args) {
      result = min(result, ...args)
      return bus
    },
    ac () {
      result = sub(0, result)
      return bus
    },
    abs () {
      result = Math.abs(result)
      return bus
    },
    round (numDigits, upOrDown) {
      result = round(result, numDigits, upOrDown)
      return bus
    },
    over () {
      return result
    },
    money (prefix, def) {
      return money(result, prefix, def)
    },
    moneyNum (prefix, def) {
      return moneyNum(result, prefix, def)
    }
  }
  return bus
}
